package com.aistar.controller;

import com.aistar.pojo.ProjectExperience;
import com.aistar.service.ProjectExperienceService;
import com.aistar.util.DateUtil;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Controller
public class projectExperienceController {
    @Autowired
    private ProjectExperienceService projectExperienceService;

    @PostMapping("/projectExperience")
    @ResponseBody
    protected ServerResponse add(Integer resumeId,String companyName,String startprojecttime,String endprojectTime,String projectname,
                       String projectDescription,String dutyDescription) {

        //数据类型转换
        Date startProjectTime =  DateUtil.string2DateYM(startprojecttime);
        Date endProjectTime =  DateUtil.string2DateYM(endprojectTime);

        //封装成ProjectExperience 对象
        ProjectExperience experience = new ProjectExperience();
        experience.setResumeId(resumeId);
        experience.setProjectStartTime(startProjectTime);
        experience.setProjectEndTime(endProjectTime);
        experience.setProjectCompanyName(companyName);
        experience.setProjectName(projectname);
        experience.setProjectContentDesc(projectDescription);
        experience.setProjectDutyDesc(dutyDescription);
        experience.setStatus(0);

        //service

        ServerResponse serverResponse = projectExperienceService.add(experience);
        if(serverResponse.getStatus() ==1){
            serverResponse =  projectExperienceService.getLatestByResumeId(resumeId);
        }
       return serverResponse;

    }

    @PutMapping("/projectExperience")
    @ResponseBody
    protected ServerResponse update(Integer projectExperiId, String companyName,String startprojecttime,String endprojectTime,String projectname,
                          String projectDescription,String dutyDescription) {
        //数据类型转换
        Date startProjectTime =  DateUtil.string2DateYM(startprojecttime);
        Date endProjectTime =  DateUtil.string2DateYM(endprojectTime);

        //ProjectExperience 对象
        ProjectExperience experience = new ProjectExperience();
        experience.setProjectExperiId(projectExperiId);
        experience.setProjectStartTime(startProjectTime);
        experience.setProjectEndTime(endProjectTime);
        experience.setProjectCompanyName(companyName);
        experience.setProjectName(projectname);
        experience.setProjectContentDesc(projectDescription);
        experience.setProjectDutyDesc(dutyDescription);
        experience.setStatus(0);

        //service
        ServerResponse serverResponse = projectExperienceService.modify(experience);
        if(serverResponse.getStatus() ==1){
            serverResponse =  projectExperienceService.getById(projectExperiId);
        }
       return serverResponse;

    }

    @GetMapping("/projectExperience/{id}")
    protected ServerResponse detail(@PathVariable("id") Integer projectExperiId) {
        ServerResponse serverResponse =  projectExperienceService.getById(projectExperiId);
       return serverResponse;
    }

}
